﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace gTrans
{
	public partial class Form1 : Form
	{
		private static int LangID;
		private static string Lang = "client=t&sl=auto&tl=ru&text={0}";

		public Form1()
		{
			InitializeComponent();
			var argv = Environment.GetCommandLineArgs();
			if (argv.Length > 1)
			{
				var langs = Regex.Match(argv[1], @"(\w+?)\d(\w+)", RegexOptions.IgnoreCase);
				if (langs.Success)
				{
					Lang = string.Format("client=t&sl={0}&tl={1}&text={{0}}", langs.Groups[1].Value, langs.Groups[2].Value);
					button1.Text = argv[1];
				}
			}
		}

		private static string Translate(string text)
		{
			var loginReq = (HttpWebRequest)WebRequest.Create("http://www.google.com/translate_a/t");
			loginReq.UserAgent = 
				"Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7";
			loginReq.Method = "POST";
			loginReq.ContentType = "application/x-www-form-urlencoded;";

			var someBytes = Encoding.UTF8.GetBytes(string.Format(Lang, text));
			loginReq.ContentLength = someBytes.Length;

			var newStream = loginReq.GetRequestStream();
			newStream.Write(someBytes, 0, someBytes.Length);
			newStream.Close();

			var loginResult = (HttpWebResponse)loginReq.GetResponse();
			var loginRs = loginResult.GetResponseStream();
			var encode = Encoding.UTF8;

			var loginSr = new StreamReader(loginRs, encode);
			var result = loginSr.ReadToEnd();
			loginSr.Close();

			loginResult.Close();

			return result;
		}

		private void TranslateButton_Click(object sender, EventArgs e)
		{
			if(textBox3.Text.Length == 0) return;
			var translate = Translate(textBox3.Text);
			translate = translate.StartsWith("[") && translate.EndsWith("]") ? 
				Regex.Match(translate, @"""(.*?)"",").Groups[1].Value : 
				translate.Remove(0, 1).Remove(translate.Length - 2, 1);

			textBox1.Text = translate.Replace("\\r\\n", "\r\n").Replace("\\t", "\t").Replace("\\\"", "\"");
			
			if (checkBox1.Checked)
			{
				Clipboard.SetText(textBox1.Text);
				textBox3.Focus();
				textBox3.SelectAll();
			}
			else
			{
				textBox1.Focus();
				textBox1.SelectAll();
			}
		}

		private void ChangeLanguageButton_Click(object sender, EventArgs e)
		{
			if (LangID < 2)
			{
				LangID++;
			}
			else
			{
				LangID = 0;
			}

			switch (LangID)
			{
				case 0:
					{
						button1.Text = "En2Ru";
						Lang = "client=t&sl=en&tl=ru&text={0}";
						break;
					}
				case 1:
					{
						button1.Text = "Ru2En";
						Lang = "client=t&sl=ru&tl=en&text={0}";
						break;
					}
				case 2:
					{
						button1.Text = "Auto2Ru";
						Lang = "client=t&sl=auto&tl=ru&text={0}";
						break;
					}
			}

			textBox3.Focus();
		}

		private void TextBox_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.Modifiers == Keys.Control && e.KeyCode == Keys.Enter)
			{
				e.SuppressKeyPress = true;
				this.TranslateButton_Click(sender, null);
			}
		}

		private void checkBox1_CheckedChanged(object sender, EventArgs e)
		{
			if (checkBox1.Checked && textBox1.Text.Length != 0)
			{
				Clipboard.SetText(textBox1.Text);
			}
		}
	}
}